<?php
// function __autoload($class_name) {
//         include 'class.' . strtolower($class_name) . '.php';
// }

class UsulanDao {

	public function getAll($id_kewenangan,$kdkanwil){
		$db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_usulan ";
        //if($id_kewenangan!='1'){
            $query.=" WHERE unit='$kdkanwil'";
        //}

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()>=1){
            return $result->fetchAll();
        }

        return false;
	}

    public function getNotifikasi($kdkanwil){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_usulan ";
        //if($id_kewenangan!='1'){
            $query.=" WHERE unit='$kdkanwil' AND level_usulan='4'";
        //}

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()>=1){
            return $result->fetchAll();
        }

        return false;
    }


    public function getAllUsulan(){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_usulan ";
        //if($id_kewenangan!='1'){
        //    $query.=" WHERE kdkanwil='$kdkanwil'";
        //}

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()>=1){
            return $result->fetchAll();
        }

        return false;
    }

    public function getAllNotifikasi(){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_usulan WHERE level_usulan='7'";
        //if($id_kewenangan!='1'){
        //    $query.=" WHERE kdkanwil='$kdkanwil'";
        //}

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()>=1){
            return $result->fetchAll();
        }

        return false;
    }




    public function getJmlPegawai($id_usulan){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_det_usulan WHERE id_usulan='$id_usulan'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()>=1){
            return $result->rowCount();
        }

        return false;
    }

    public function getAllPegawai($id_usulan){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_det_usulan WHERE id_usulan='$id_usulan'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()>=1){
            return $result->fetchAll();
        }

        return false;
    }

    public function getAllUsulanPeg(){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_det_usulan";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()>=1){
            return $result->fetchAll();
        }

        return false;
    }

    public function getAllUsulanPegFilter($id_level){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_det_usulan ";
        if($id_level=='6'){
            $query.= " WHERE id_level='$id_level' OR id_level='5'";
        }else{
            $query.= " WHERE id_level='$id_level'";
        }

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()>=1){
            return $result->fetchAll();
        }

        return false;
    }

	public function getAllByKanwil($unit){
		$db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_usulan WHERE unit='$unit'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()>=1){
            return $result->fetchAll();
        }

        return false;
	}

	public function getById(Usulan $usulan){
		$db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_usulan WHERE id_usulan='$usulan->id_usulan'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return $result->fetch();
        }

        return false;
	}

    public function createUsulan(Usulan $usulan){
        $db= Database::getInstance();
        $con=$db->getConnection(1);

        $date_created=date('Y-m-d H:i:s');

        $query="INSERT INTO t_usulan(no_surat,unit,tgl_surat,perihal,level_usulan,date_created,user_creating) ";
        $query.=" VALUES('$usulan->no_surat','$usulan->unit','$usulan->tgl_surat','$usulan->perihal',
            '$usulan->level_usulan','$date_created','$usulan->id_user')";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

	public function updateUsulan(Usulan $usulan){
		$db=Database::getInstance();
        $con=$db->getConnection(1);

        $date_updated=date('Y-m-d H:i:s');

        $query="UPDATE t_usulan SET no_surat='$usulan->no_surat', tgl_surat='$usulan->tgl_surat',perihal='$usulan->perihal', 
        	date_updated='$date_updated',user_updating='$usulan->id_user' WHERE id_usulan='$usulan->id_usulan'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
	}

    public function deleteUsulan($id_usulan){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="DELETE FROM t_usulan WHERE id_usulan='$id_usulan'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

    public function kirimUsulan($id_usulan){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="UPDATE t_usulan set level_usulan='7' WHERE id_usulan='$id_usulan'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

    public function terimaUsulan($id_usulan){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="UPDATE t_usulan set level_usulan='4' WHERE id_usulan='$id_usulan'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }
}

class Usulan {
	var $id_usulan;
	var $id_user;
	var $unit;
	var $no_surat;
	var $tgl_surat;
	var $perihal;
	var $level_usulan;
	var $id_tanggapan;
}

?>